#include "widget.h"
#include "ui_widget.h"


Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
    ,ui_stu(nullptr)
    ,ui_book(nullptr)
    ,ui_borr(nullptr)
{
    ui->setupUi(this);
    //ui_stu = new StudetInformation();

//    stringlist.clear();
//     stringlist = fileWriteAndRead().readAndParseFile("book.txt");
//    for(int i = 0; i < stringlist.size();i++)
//    {
//        Book bok(stringlist[i]);
//        this->book.push_back(bok);
//    }
//    qDebug()<<stringlist;
//    stringlist.clear();
//   stringlist = fileWriteAndRead().readAndParseFile("borrowing.txt");
//    for(int i = 0; i < stringlist.size();i++)
//    {
//        Borrowing bor(stringlist[i]);
//        this->borrowing.push_back(bor);
//    }

    //fileWriteAndRead().writeDataToFile("book.txt",stringlist);
    //qDebug()<<stringlist;
//    for(int i = 0; i <10;i++)
//    {
//        Book bok("2031053231","1597894562","张三",50.9,40,QDate::currentDate(),"北京市中南海");
//        book.push_back(bok);
//    }
//    for(int i = 0; i <10;i++)
//    {
//        QString cnt = "20310532" + QString::number(i).rightJustified(2,'0');
//        Student stu(cnt,"张三","网络工程","网络2002",i);
//        student.push_back(stu);
//    }
////    qDebug()<<student[0].getStudent();
//    qDebug()<<student.size();


}


Widget::~Widget()
{
    //    QVector<QStringList> cnt;
    //    for(Student& stu:this->student)
    //    {
    //        cnt.push_back(stu.getStudent());
    //    }
    //    qDebug()<<cnt;
    //    fileWriteAndRead().writeDataToFile("student.txt",cnt);
    //    cnt.clear();



//    for(Borrowing& bor:this->borrowing)
//    {
//        cnt.push_back(bor.getBorrowing());
//    }
//    fileWriteAndRead().writeDataToFile("borrowing.txt",cnt);
//    cnt.clear();
    delete ui_borr;
    delete ui_stu;
    delete ui;
}




void Widget::on_pushButton_student_clicked()
{
    if(ui_stu == nullptr)
    {
        this->ui_stu = new StudetInformation();
        connect(ui_stu,&StudetInformation::is_close,this,&Widget::close_student);
    }
    ui_stu->show();
    ui_stu->raise();
    ui_stu->activateWindow();
}

void Widget::closeEvent(QCloseEvent *event)
{
    if(ui_stu !=nullptr)
    {
        ui_stu->close();
        delete ui_stu;
        ui_stu = nullptr;
    }
    if(ui_book!=nullptr)
    {
        ui_book->close();
        delete ui_book;
        ui_book = nullptr;
    }
    if(ui_borr !=nullptr)
    {
        ui_borr->close();
        delete ui_borr;
        ui_borr = nullptr;
    }
    QWidget::closeEvent(event);
}

void Widget::on_pushButton_book_clicked()
{
    if(ui_book == nullptr)
    {
        this->ui_book = new bookInformation();
        connect(ui_book,&bookInformation::is_close,this,&Widget::close_book);
    }
    ui_book->show();
    ui_book->raise();
    ui_book->activateWindow();
}

void Widget::on_pushButton_borrowing_clicked()
{
    if(ui_borr == nullptr)
    {
        this->ui_borr = new borrInformation();
         connect(ui_borr,&borrInformation::is_close,this,&Widget::close_borrowing);
    }
    ui_borr->show();
    ui_borr->raise();
    ui_borr->activateWindow();
}

void Widget::close_student()
{
    ui_stu->disconnect();
    delete ui_stu;
    ui_stu=nullptr;
}

void Widget::close_book()
{
    ui_book->disconnect();
    delete ui_book;
    ui_book=nullptr;
}

void Widget::close_borrowing()
{
    ui_borr->disconnect();
//    qDebug()<<__LINE__;
    delete ui_borr;
    ui_borr=nullptr;
//    qDebug()<<__LINE__;
}
